﻿using System;
using System.Threading.Tasks;
using UnityEngine;

public partial class RequestServerController
{
    int m_CurUser;
    public int curUser => m_CurUser;
    public void ReqLogin(Action onLoginSuc, Action onTimeout)
    {
        ReqLogin(m_CurUser, onLoginSuc, onTimeout);
    }
    public void ReqLogin(int user,Action onLoginSuc,Action onTimeout) 
    {
        m_CurUser = user;

        Request_Login requestBase = new Request_Login();
        requestBase.loginReqData = Request_Login.GetDefaultData();// new LoginRequestData();
        UserInfo userInfo = MGame.instance.MUser.userInfo;

        bool needRetIcon = true;
        bool needRetTex1 = true;
        bool needRetTex2 = true;
        bool needRetTex3 = true;
        bool needRetTex4 = true;
        bool needRetTex5 = true;
        bool needRetTex6 = true;

        //bool needRetIcon = Tex.IsNull(userInfo.icon);
        //bool needRetTex1 = Tex.IsNull(userInfo.tex1);
        //bool needRetTex2 = Tex.IsNull(userInfo.tex2);
        //bool needRetTex3 = Tex.IsNull(userInfo.tex3);
        //bool needRetTex4 = Tex.IsNull(userInfo.tex4);
        //bool needRetTex5 = Tex.IsNull(userInfo.tex5);
        //bool needRetTex6 = Tex.IsNull(userInfo.tex6);
        //requestBase.loginReqData.needReturnIcon = needRetIcon;
        //requestBase.loginReqData.needReturnTex1 = needRetTex1;
        //requestBase.loginReqData.needReturnTex2 = needRetTex2;
        //requestBase.loginReqData.needReturnTex3 = needRetTex3;
        //requestBase.loginReqData.needReturnTex4 = needRetTex4;
        //requestBase.loginReqData.needReturnTex5 = needRetTex5;
        //requestBase.loginReqData.needReturnTex6 = needRetTex6;

        GameDebug.Log($"needRetIcon:{needRetIcon}  needRetTex1:{needRetTex1} needRetTex2:{needRetTex2} needRetTex3:{needRetTex3}");

        if (string.IsNullOrWhiteSpace(GameSettingViewController.username))
        {
            requestBase.loginReqData.uuid = GetUUID(user);
        }
        else
        {
            requestBase.loginReqData.uuid = GameSettingViewController.username;
            requestBase.loginReqData.password = GameSettingViewController.password;
        }
        GameDebug.Log($"Request_Login uuid:{requestBase.loginReqData.uuid} ");

        requestBase.loginReqData.isRobot = false;

        requestBase.onResponse = delegate (ResponseBase response)
        {
            //Response_Login login = response as Response_Login;

            var login = response.responseData as LoginResponseData;

            GameDebug.Log($"Request_Login onResponse:");

            MGame.instance.MUser.InitWithServerData(login.user, needRetIcon, needRetTex1, needRetTex2, needRetTex3,
                needRetTex4, needRetTex5, needRetTex6);

            onLoginSuc?.Invoke();
        };
        requestBase.onTimeOut = delegate (RequestBase req)
        {
            GameDebug.Log("Request_Login onTimeOut ");
            onTimeout?.Invoke();
        };

        requestBase.clientSocket = MGame.instance.clientSocket;
        requestBase.timeOutSeconds = 12;
        requestBase.Send();
    }

    public static bool EditorTest;
    private const string GUIDKey = "KEY_GUID";
    public string GetUUID(int user)
    {
#if UNITY_EDITOR
        if (EditorTest)
            return GetDeviceId() + "_" + user;
        else
            return guid + "_" + user;
#endif

        return GetDeviceId() + "_" + user;
    }
    string guid
    {
        get
        {
            if (PlayerPrefs.HasKey(GUIDKey))
            {
                return PlayerPrefs.GetString(GUIDKey);
            }
            else
            {
                string guid = Guid.NewGuid().ToString()
                    .Replace("-", "");

                PlayerPrefs.SetString(GUIDKey, guid);

                return guid;
            }
        }
    }
    public static string GetDeviceId()
    {
        bool is_iOS = UnityEngine.Application.platform == UnityEngine.RuntimePlatform.IPhonePlayer;

        string id = UnityEngine.SystemInfo.deviceUniqueIdentifier;
#if UNITY_IOS
        id = is_iOS ? UnityEngine.iOS.Device.advertisingIdentifier : id; // iOS 目前使锟矫癸拷锟斤拷识锟斤拷
#endif
        return id;
    }
}
